package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.PullStrategyDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.PullStrategy;
import cn.b.sky.mes.mm.model.condition.PullStrategyCondition;
import cn.b.sky.mes.mm.service.PullStrategyService;
import cn.b.sky.common.utils.DatabaseUtils;

@Service("pullStrategyService")
public class PullStrategyServiceImpl implements PullStrategyService {

  private static final Logger LOG = LoggerFactory.getLogger(RequirementTriggerModeServiceImpl.class);

  @Resource
  PullStrategyDao pullStrategyDao;

  @Override
  public List<PullStrategy> pullStrategyListByCondition(PullStrategyCondition condition) {
    if (null == condition) {
      return null;
    }
    return pullStrategyDao.pullStrategyListByCondition(condition);
  }

  @Override
  public int pullStrategyCountByCondition(PullStrategyCondition condition) {
    if (null == condition) {
      return 0;
    }
    return pullStrategyDao.pullStrategyCountByCondition(condition);
  }

  @Override
  public void insertPullStrategy(PullStrategy pullStrategy) {
    if (null != pullStrategy) {
      pullStrategyDao.insertPullStrategy(pullStrategy);
    }
  }

  @Override
  public void updatePullStrategy(PullStrategy pullStrategy) {
    if (null != pullStrategy) {
      pullStrategyDao.updatePullStrategy(pullStrategy);
    }
  }

  @Override
  public String detelePullStrategy(String siteCode, String pullStrategyCode) {
    String message = null;
    try {
      pullStrategyDao.detelePullStrategy(siteCode, pullStrategyCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
        message = "dataBaseOperationPkFail.M";
      }
    }
    return message;
  }

  @Override
  public PullStrategy getPullStrategy(String siteCode, String pullStrategyCode) {
    if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(pullStrategyCode)) {
      return pullStrategyDao.getPullStrategy(siteCode, pullStrategyCode);
    }
    return null;
  }

  @Override
  public List<PullStrategy> selectAllPullStrategy(boolean isContainLock) {
    return pullStrategyDao.selectAllPullStrategy(isContainLock);
  }

  @Override
  public PullStrategy getPullStrategyByMaterial(String siteCode, String materialCode) {
    return pullStrategyDao.getPullStrategyByMaterial(siteCode, materialCode);
  }

}
